<?php
namespace Home\Controller;

use Util\AccessController;

class OrderController extends AccessController {


    /*
     * 获取添加订单数据样例
     */
    public function GETDATA() {
        $data = array(1, 2);
        foreach ($data as $k => $v) {
            $temp['book_gg_id'] = 30 + $k;
            $temp['num'] = 1;
            $data1[] = $temp;
        }
        $datas['books'] = $data1;
        $datas['user_id'] = 'WX_5a1245a57420c_272';
        $datas['card_id'] = 4;
        $datas['order_phone'] = 13631297676;
        $datas['order_address'] = '收件人地址';
        $datas['order_address_name'] = '收件人名称';
        $datas['order_money'] = 120;
        $this->ajaxReturn($datas, 'JSON');
    }

    /*
     * 添加订单
     * 参数
     * GETDATA接口样例
     *返回值
     * -1;//优惠劵不存在
     * -2;//库存不足
     * -3;//库存修改失败
     * -4;//订单主表添加失败
     * -5;//订单书籍添加失败
     * -6;//优惠劵修改失败
     */
    public function addOrder() {
        $datas = $_POST;
        $OID = 'WXOID_' . uniqid() . rand(10, 99);//订单ID;//订单ID
        $LS = Service('Order', 'order');
        $OK = $LS->AddOrder($datas, $OID);
        $data['res'] = $OK;
        $data['oid'] = $OID;
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 获取用户所有订单列表
     * post
     * USER_ID 用户user_id
     * (选填)分页页数PAGE=1
     * (选填)分页每页显示数据量PAGE_NUM=10
     * (选填)订单类型STATE=1（1：代付款，2：待发货，3：待签收，4：退换中，5：已经完成,6 :已关闭,-1伪删除）
     */
    public function GetMyOrderList() {
        $where['ts_order.user_id'] = $_POST['USER_ID'];
        $where['ts_order.order_leixing'] = 2;
        $_POST['STATE'] ? $where['ts_order.order_type'] = $_POST['STATE'] : $where['ts_order.order_type'] = array('neq', -1);
        $limit = GET_LIMIT($_POST['PAGE'], $_POST['PAGE_NUM']);
        $LS = Service('Order', 'order');
        $data = $LS->GetAllOrder($where, $limit);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 获取订单详情
     * post
     * ORDER_ID  订单id
     */
    public function GetOrderData() {
        $where['ts_order.order_id'] = $_POST['ORDER_ID'];
        $where['ts_order.order_leixing'] = 2;
        $LS = Service('Order', 'order');
        $data = $LS->GetOrderData($where);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
    * 修改订单状态接口（关闭  确认收货）
    * post
    * ORDER_ID  订单id
    * TYPE （-1关闭 1 确认收货 -2伪删除）
    */
    public function UpdateOrderState() {
        $where['ts_order.order_id'] = $_POST['ORDER_ID'];
        $LS = Service('Order', 'order');
        if ($_POST['TYPE'] == -1) {
            $save['order_type'] = 6;
            $updateNum = $LS->rollbackBookNum($_POST['ORDER_ID']);
        } else if ($_POST['TYPE'] == 1) {
            $save['order_type'] = 5;
            $updateNum = 1;
        } else if ($_POST['TYPE'] == -2) {
            $save['order_type'] = -1;
        } else {
            $DATA = ResErrorData();
            $this->ajaxReturn($DATA, 'JSON');
        }
        $LS1 = Service('Order', 'order');
        $data = $LS1->update($where, $save);
        //库存修改
        if ($updateNum) {
            $DATA = ResData($data);
            $this->ajaxReturn($DATA, 'JSON');
        } else {
            $DATA = ResErrorData(6);
            $this->ajaxReturn($DATA, 'JSON');
        }

    }

    /*
     * 获取运费模板
     */
    public function GetPostage(){
        $LS = Service('M','postage');
        $data = $LS->find(array('STATE'=>1));
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }
    /*
     * 取消订单
     */
    public function QxOrder($order_id){
        $LS = Service('Order', 'order');
        $updateNum = $LS->rollbackBookNum($order_id);
        echo $updateNum;
    }

}